<?php

namespace app\admin\logic;

use nx\Data;
use think\Cache;
use app\admin\model\Depar as Depar_model;

class Depar
{
    public $depar_model = '';

    /**
     * 获取模型
     * @return Depar_model|string
     */
    public function getModel(){
        if (empty($this->depar_model)){
            $this->depar_model = new Depar_model();
        }
        return $this->depar_model;
    }

    /**
     * 获取所有的部门部门（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function deparsToText(){
        $depars = self::getAllDepars();
        $depars = collect($depars)->column('name','id');
        return $depars;
    }

//    /**
//     * 获得所有的部门部门（从缓存中）
//     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
//     */
//    public function getAllDepars(){
//        if (!empty(Cache::get('allDepars'))){
//            return Cache::get('allDepars');
//        }else{
//            $depars = Depar_model::all();
//            Cache::set('allDepars',$depars);
//            return $depars;
//        }
//    }


    /**获得所有的部门部门（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllDepars()
    {
        if (!empty(Cache::get('allDepars'))) {
            return Cache::get('allDepars');
        } else {
            $depars = Depar_model::all();
            $depars = collection($depars)->append(['status_text'])->toArray();
            $depars = array_merge($depars,[['id'=>0,'name'=>'无','pid'=>100000]]);
            Cache::set('allDepars', $depars);
            return $depars;
        }
    }

    public static function getPidById($depar_id)
    {
        $depars = self::getAllDepars();
//        dump($depars);
//        return collect($depars)->where('id',$depar_id)->collapse()->get('pid');
        $aaa = collect($depars)->where('id',$depar_id)->collapse()->get('pid');
        dump($aaa);
        return $aaa;
    }
    /**
     * 将所有部门进行树形排列
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getAllDeparToTree(){
        $depars = $this->getAllDepars();
        $depars = Data::tree($depars,'name','id','pid');
        return $depars;
    }

    /**
     * 获取启用的状态
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getEnabledDepars(){
        $states = $this->getAllDepars();
        $states = collect($states)->where('status',1)->all();
        $states = collection($states)->toArray();
        $states = array_merge($states);
        return $states;
    }

    /**
     * 将所有部门进行树形排列
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getEnabledDeparsToTree(){
        $depars = $this->getEnabledDepars();
        $depars = Data::tree($depars,'name','id','pid');
        return $depars;
    }


    /**
     * 获取某部门的所有下属部门
     * @param $depar_id
     * @return array|mixed
     */

//    public function getNextDepars($depar_id){
//        $data = $this->getAllDepars();
//        $depars = Data::channelList($data,$depar_id,'— ','id','pid');
//        return $depars;
//    }


    public function addDepar($data){
        if(empty($data)){
            return ['result'=>false,'msg'=>'数据丢失'];
        }
        $result = $this->getModel()->validate(true)->save($data);
        if(false === $result){
            return ['result'=>false,'msg'=>$this->getModel()->getError()];
        }else{
            Cache::rm('allDepars');
            return ['result'=>true,'msg'=>'部门添加成功'];
        }
    }

    public function editDepar($data){
        if(empty($data['id'])){
            return ['result'=>false,'msg'=>'ID丢失'];
        }
        isset($data['status'])?:$data['status']=1;
        $result = $this->getModel()->allowField(true)->validate(true)->isUpdate(true)->save($data);
        if(false === $result){
            return ['result'=>false,'msg'=>$this->getModel()->getError()];
        }else{
            Cache::rm('allDepars');
            return ['result'=>true,'msg'=>'部门修改成功'];
        }
    }

    public function delDepar($id){
        if ( empty($id) ) {
            return ['result'=>false,'msg'=>'请选择要操作的数据'];
        }
        if(Depar_model::get($id)){
            if(Depar_model::destroy($id)){
                Cache::rm('allDepars');
                return ['result'=>true,'msg'=>'删除成功'];
            } else {
                return ['result'=>false,'msg'=>'删除失败'];
            }
        }else{
            return ['result'=>false,'msg'=>'要删除的数据不存在'];
        }
    }

  





}
